package net.kucoe.macarun.sample;

import static net.kucoe.macarun.Macarun.match;
import net.kucoe.macarun.*;

/**
 * {@link Macarun} sample.
 * 
 * @author Vitaliy Basyuk
 */
public class MacarunSample {
    
    /**
     * Main method
     * 
     * @param args
     */
    public static void main(final String[] args) {
        MacarunSample sample = new MacarunSample();
        Naming naming = new Naming("name");
        sample.doMatch(naming);
        Counting counting = new Counting(1);
        sample.doMatch(counting);
        Coloring coloring = new Coloring("red", "#f00");
        sample.doMatch(coloring);
        sample.doMatch(sample);
    }
    
    private void doMatch(final Object object) {
        Match match = match(object);
        match.inCase(Coloring.class).with("name", String.class).with("color", String.class)
                .check(new Check<Coloring>() {
                    public boolean check(final Coloring coloring) {
                        return coloring.getName() != null && coloring.getColor() != null;
                    }
                }).run(new Run<Coloring, Void>() {
                    public Void run(final Coloring coloring) {
                        System.out.println("From Coloring: name - " + coloring.getName() + " color - "
                                + coloring.getColor());
                        return null;
                    }
                }).breaking();
        match.inCase(Naming.class).with("name", String.class).check(new Check<Naming>() {
            public boolean check(final Naming naming) {
                return naming.getName() != null;
            }
        }).run(new Run<Naming, Void>() {
            public Void run(final Naming naming) {
                System.out.println("From Naming: " + naming.getName());
                return null;
            }
        }).breaking();
        match.inCase(Counting.class).with("count", int.class).check(new Check<Counting>() {
            public boolean check(final Counting counting) {
                return counting.getCount() > 0;
            }
        }).run(new Run<Counting, Void>() {
            public Void run(final Counting counting) {
                System.out.println("From Counting: " + counting.getCount());
                return null;
            }
        }).breaking();
        match.inDefault().run(new Run<Object, Void>() {
            public Void run(final Object object) {
                System.out.println("From Undefined: " + String.valueOf(object));
                return null;
            }
        }).breaking();
        match.run();
    }
    
}
